package com.hongkzh.www.conmon;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import io.rong.RongCloud;
import io.rong.methods.user.User;
import io.rong.models.Result;
import io.rong.models.group.GroupMember;
import io.rong.models.group.GroupModel;
import io.rong.models.response.TokenResult;
import io.rong.models.user.UserModel;

import java.util.List;

public class RongCloudTool {


    private static RongCloud rongCloud = null;

    /**

     */
    public static RongCloud getRongCloud() {
        if (rongCloud == null) {
            rongCloud = RongCloud.getInstance(PropKit.get("rongCloud.appkey"), PropKit.get("rongCloud.appSecret"));
        }
        return rongCloud;
    }
   public static TokenResult registerUser(Record r) throws Exception {
       User User = getRongCloud().user;
        UserModel user = new UserModel()
            .setId(r.get("id"))
            .setName(r.get("name"))
            .setPortrait(r.get("headImg"));
        TokenResult result = User.register(user);
        if(result.getCode()==200) {
            r.set("token", result.getToken());
             saveUser(r);
        }
      //System.out.println("getToken:  " + result.toString());


      return result;
   }
    public static void updateUser(Record r) throws Exception {
        User User = getRongCloud().user;
        UserModel user = new UserModel()
                .setId(r.get("id"))
                .setName(r.get("name"))
                .setPortrait(r.get("headImg"));
         Result result = User.update(user);
    }
   public static void saveUser(Record r){
       SqlPara sqlPara=Db.getSqlPara("user.saveChattoken", StrKit.getRandomUUID(),r.get("id"),r.get("token"));
       Db.update(sqlPara);
   }

    public static void createGroup(String id,String uid, String circleName) throws Exception {
        GroupModel group=new GroupModel();
        group.setId(id);
        group.setName(circleName);
        GroupMember a=new GroupMember();
        a.setGroupId(id);
        a.setId(uid);
        GroupMember[] members={a};

        group.setMembers(members);
        Result result = getRongCloud().group.create(group);
        //System.out.println(result);
    }
    public static void joinGroup(String uid, String circleId) throws Exception {
        GroupModel group=new GroupModel();
        group.setId(circleId);
        group.setName("1");
        GroupMember a=new GroupMember();
        a.setGroupId(circleId);
        a.setId(uid);
        GroupMember[] members={a};

        group.setMembers(members);
        Result result = getRongCloud().group.join(group);
        //System.out.println(result);
    }
    public static void addBlacklist(String uid, List<Record> list) throws Exception {

        User User = getRongCloud().user;
        UserModel user = new UserModel();
        user.setId(uid);
        UserModel[] friendIds= new UserModel[list.size()];
        for (int i=0;i<list.size();i++) {
            friendIds[i]=new UserModel().setId(list.get(i).getStr("id"));
        }
        user.setBlacklist(friendIds);
        Result result = User.blackList.add(user);

    }
    public static void main(String[] args) throws Exception {
        //getRongCloud().group.create()
        PropKit.use("a_little_config.txt");
        //createGroup("21f9085ba48a43939286cc61a8430e56","c6ba28631e514159b1ccade146ea2c06","测试44");
        //joinGroup("1","21f9085ba48a43939286cc61a8430e56");
       /* Record r=new Record();
        r.set("id","1");
        r.set("name","1");
        r.set("portrait","http://47.95.5.235:8081/userfiles/1/images/product/2018/04/%E5%A4%B4%E5%83%8F.png");
        registerUser(r);*/
        quitGroup("1","21f9085ba48a43939286cc61a8430e56");
    }


    public static void quitGroup(String uid, String circleId) throws Exception {
        GroupModel group=new GroupModel();
        group.setId(circleId);
        group.setName("1");
        GroupMember a=new GroupMember();
        a.setGroupId(circleId);
        a.setId(uid);
        GroupMember[] members={a};

        group.setMembers(members);
        Result result = getRongCloud().group.quit(group);
        System.out.println(result);
    }

    public static void dismissGroup(String uid, String circleId) throws Exception {
        GroupModel group=new GroupModel();
        group.setId(circleId);
        group.setName("1");
        GroupMember a=new GroupMember();
        a.setGroupId(circleId);
        a.setId(uid);
        GroupMember[] members={a};

        group.setMembers(members);
        Result result = getRongCloud().group.dismiss(group);
        System.out.println(result);
    }
}
